﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class admin_param : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            BindDDL();
            BindUpdateParamInfo();
            BindNotice();
        }
        private void BindNotice()
        {
            noticefail.Visible = false;
            noticesuccess.Visible = false;
        }
        private void BindUpdateParamInfo()
        {
            UpdateParamInfo up = UpdateParamInfoLogic.GetUpdateParamInfo();
            ddlWorksUpdateSpanTime.SelectedValue = up.WorksUpdateSpanTime.ToString();
            ddlNewsSpanTime.SelectedValue = up.NewsSpanTime.ToString();
            ddlNewsUpdateSpanTime.SelectedValue = up.NewsUpdateSpanTime.ToString();
            ddlWorksSpanTime.SelectedValue = up.WorksSpanTime.ToString();
            txbNewsTop.Text = up.NewsTop.ToString();
            txbWorksTop.Text = up.WorksTop.ToString();
        }
        private void BindDDL()
        {
            List<int> list = new List<int>();
            for (int i = 0; i <= 100; i++)
            {
                list.Add(i);
            }
            ddlNewsSpanTime.DataSource = list;
            ddlNewsSpanTime.DataBind();
            ddlNewsUpdateSpanTime.DataSource = list;
            ddlNewsUpdateSpanTime.DataBind();
            ddlWorksSpanTime.DataSource = list;
            ddlWorksSpanTime.DataBind();
            ddlWorksUpdateSpanTime.DataSource = list;
            ddlWorksUpdateSpanTime.DataBind();
        }
        private UpdateParamInfo createUpdateParam()
        {
            UpdateParamInfo up = new UpdateParamInfo();
            up.NewsTop = StringToInt(txbNewsTop.Text,5);
            up.WorksTop = StringToInt(txbWorksTop.Text, 5);
            up.NewsSpanTime = StringToInt(ddlNewsSpanTime.SelectedValue,7);
            up.NewsUpdateSpanTime = StringToInt(ddlNewsUpdateSpanTime.SelectedValue, 3);
            up.WorksSpanTime = StringToInt(ddlWorksSpanTime.SelectedValue,7);
            up.WorksUpdateSpanTime = StringToInt(ddlWorksUpdateSpanTime.SelectedValue, 3);
            up.NewsUpdateTime = DateTime.Now;
            up.WokrsUpdateTime = DateTime.Now;
            return up;
        }
        private int StringToInt(string tmp,int first){
            int rtn = first;
            try
            {
                rtn = Int32.Parse(tmp);
            }
            catch { }
            return rtn;
        }

        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            UpdateParamInfo up = createUpdateParam();
            int isSuccess = UpdateParamInfoLogic.UpdateTheParamInfo(up);
            if (isSuccess != 1)
            {
                ChangeStatus(false);
            }
            else {
                ChangeStatus(true);
            }
        }
        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefail.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();", true);
        }
    }
}
